package com.itany.book.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.constant.Constant;
import com.itany.exception.RequestParamsException;
import com.itany.mapper.UserMapper;
import com.itany.pojo.User;
import com.itany.service.UserService;
import com.itany.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;


    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public PageInfo<User> findUserAll(Integer page, Integer rows, String keyWord, String flag) throws RequestParamsException {
        PageHelper.startPage(page, rows);
        List<User> list;
        Integer flagInt = null;
        try {
            if (!StrUtil.isEmptyIfStr(flag)) {
                flagInt = Integer.parseInt(flag);
            }
        } catch (NumberFormatException e) {
            throw new RequestParamsException("请求参数格式错误");
        }
        list = userMapper.findUserAll(StringUtils.escapeStr(keyWord), flagInt);
        return new PageInfo<>(list);
    }

    /**
     * 修改用户信息
     *
     * @param id       用户id
     * @param name     用户名
     * @param phone    电话
     * @param email    邮箱
     * @param interest 兴趣
     */
    @Override
    public void modifyUser(String id, String name, String phone, String email, String interest) throws RequestParamsException {
        if (StrUtil.isEmpty(id) || StrUtil.isEmpty(name) || StrUtil.isEmpty(phone) || StrUtil.isEmpty(email) || StrUtil.isEmpty(interest)) {
            throw new RequestParamsException("请求参数异常");
        }
        User user = new User();
        try {
            user.setId(Integer.parseInt(id));
            user.setPhone(phone);
            user.setEmail(email);
            user.setInterest(interest);
            user.setUsername(name);
        } catch (NumberFormatException e) {
            throw new RequestParamsException("请求参数格式错误");
        }
        userMapper.modifyUser(user);
    }

    /**
     * 修改指定用户状态
     *
     * @param id     用户id
     * @param status 更新后的状态
     */
    @Override
    public void modifyStatus(String id, Integer status) throws RequestParamsException {
        if (StrUtil.isEmpty(id) || status == null) {
            throw new RequestParamsException("请求参数缺失");
        }
        if (!Constant.ENABLE_STATUS.equals(status) && !Constant.DISABLE_STATUS.equals(status)) {
            throw new RequestParamsException("请求参数格式有误");
        }
        try {
            userMapper.modifyStatus(Integer.parseInt(id), status);
        } catch (NumberFormatException e) {
            throw new RequestParamsException("请求参数格式错误");
        }
    }

}
